package main

import (
  "time"
  "gorm.io/gorm"
  "gorm.io/driver/sqlite"
)

type User struct {
  ID    uint   `json:"id"`
  Name  string `json:"Name"`
  Email string `json:"Email"`
  Createdat    time.Time `json:"Createdat"`
  Updatedat    time.Time `json:"Updatedat"`
}

func main() {
  db, err := gorm.Open(sqlite.Open("./data.sqlite"), &gorm.Config{})
  if err != nil {
    panic("failed to connect database")
  }

  // 迁移 schema
  db.AutoMigrate(&User{})

  // Create
  var c time.Time= time.Now()
  var u time.Time= time.Now()
  db.Create(&User{Name: "D42", Email: "m@sina.com",Createdat:c,Updatedat:u})

  // Read
  // var product Product
  // db.First(&product, 1) // 根据整形主键查找
  // db.First(&product, "code = ?", "D42") // 查找 code 字段值为 D42 的记录

  // // Update - 将 product 的 price 更新为 200
  // db.Model(&product).Update("Price", 200)
  // // Update - 更新多个字段
  // db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段
  // db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

  // // Delete - 删除 product
  // db.Delete(&product, 1)
}